package com.ff.neuropp.service;


import com.ff.neuropp.domain.Auth;
import com.ff.neuropp.repository.AuthRepository;
import com.ff.neuropp.util.SpecificationUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class AuthService {

    @Resource
    private AuthRepository authRepository;

    /**
     * 创建实体
     *
     * @param auth
     */
    public Auth saveAuth(Auth auth) {
        return authRepository.save(auth);
    }

    /**
     * 查找实体
     *
     * @param id
     * @return
     */
    public Auth findById(Integer id) {
        Optional<Auth> optional = authRepository.findById(id);
        if (optional.isPresent()) {
            return optional.get();
        } else {
            return null;
        }
    }

    public void deleteById(Integer id) {
        authRepository.deleteById(id);
    }


    /**
     * 分页条件查询
     *
     * @param page
     * @param size
     * @param param
     * @return
     */
    public Page<Auth> findPage(int page, int size, Map<String, Object> param) {
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "id";
        Pageable pageable = new PageRequest(page - 1, size, direction, sort);
        return authRepository.findAll(SpecificationUtil.buildSpecification(param, Auth.class), pageable);
    }

    /**
     * 根据条件查询List
     */
    public List<Auth> findList(Map<String, Object> param) {
        return authRepository.findAll(SpecificationUtil.buildSpecification(param, Auth.class));
    }

    public Auth findByUrl(String url) {
        return authRepository.findByUrl(url);
    }

}
